package com.qiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiku.constants.SystemConstants;
import com.qiku.entity.Comment;
import com.qiku.entity.ResponseResult;
import com.qiku.mapper.CommentMapper;
import com.qiku.service.CommentService;
import com.qiku.service.UserService;
import com.qiku.utils.SecurityUtils;
import com.qiku.vo.CommentVo;
import com.qiku.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qiku.utils.BeanCopyUtils;


import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
   @Autowired
   private UserService userService;

    @Override
    public ResponseResult commentList(String commentType,Long articleId,Integer pageNum,Integer pageSize) {
        //查询对应文章的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //对articleId进行判断  (当为文章评论的时候sql才拼接articleId；即commentType为0)
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT_TYPE.equals(commentType),Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getDelFlag, SystemConstants.DEL_FLAG);
        //根评论rootId为-1
        queryWrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOT_ID);
        //评论类型
        queryWrapper.eq(Comment::getType, commentType);
        //分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        //返回当前页信息
        List<Comment> comments = page.getRecords();
        List<CommentVo> commentVoList = toCommentListVo(comments);
        //查询所有根评论对应的子评论的集合 并赋值给对应的属性即 children
        for (CommentVo commentVo : commentVoList) {
            List<CommentVo> children = getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }
        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        comment.setCreateBy(SecurityUtils.getUserId());
        comment.setUpdateBy(SecurityUtils.getUserId());
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        save(comment);
        return ResponseResult.okResult();
    }

    /**
     * 根据根评论的id查询所对应的 子评论的集合
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        List<CommentVo> commentVos = toCommentListVo(comments);
        return commentVos;
    }

    /**
     * 为CommentVo中ToCommentUserName和username赋值
     * @param comments
     * @return
     */
    private List<CommentVo> toCommentListVo(List<Comment> comments){
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(comments, CommentVo.class);
        for (CommentVo commentVo : commentVoList) {
            //通过createBy查询昵称  并赋值
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUserName(nickName);
            //通过ToCommentUserId查询用户昵称并赋值
            //ToCommentUserId不为-1才进行查询  根评论为-1
            if (commentVo.getToCommentUserId() != -1) {
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVoList;
    }
}
